var vm = new Vue({
    el:"#app",
    data(){
        return {
            //初始化参数
            Bm:'',
            Store:'',
            addplan:false,//添加计划弹框
            //查询参数start
            qDealerName:'',
            qProductName:'',
            //查询参数end
            //添加计划参数start
            PlanNo:'',
            IStore:'',
            DealerName:'',
            DealerNo:'',
            ProductName:'',
            ProductNo:'',
            ProductPrice:'',
            ProductNumber:'',
            PlanMoney:'',
            //添加计划参数end
            formItem: {
                input: '',
            },
            dealer: false, //选择经销商弹框
            product: false, //选择产品弹框
            infomodel: false, //查看明细弹框
            //选择经销商弹框表格
            JxsNameOrNo:'',
            jxscolumns: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '经销商编号',
                    key: 'DealerNo',
                    align: 'center',
                },
                {
                    title: '经销商名称',
                    key: 'Name',
                    align: 'center',
                    width:300
                },
                {
                    title: '备注',
                    key: 'Remark',
                    align: 'center',
                }
            ],
            jxsdata: [],
            //选择产品弹框表格
            ProductNameOrNo:'',
            productcolumns: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '产品编号',
                    key: 'ProductNo',
                    align: 'center',
                },
                {
                    title: '产品名称',
                    key: 'Name',
                    align: 'center',
                },
                {
                    title: '产品品牌',
                    key: 'Brand',
                    align: 'center',
                },
                {
                    title: '产品规格',
                    key: 'Specification',
                    align: 'center',

                },
                {
                    title: '产品型号',
                    key: 'Type',
                    align: 'center',
                },
                {
                    title: '产品单位',
                    key: 'Unit',
                    align: 'center',
                },
                {
                    title: '包装规格',
                    key: 'Package',
                    align: 'center',
                },
                {
                    title: '商品条码',
                    key: 'BarCode',
                    align: 'center',
                },
                {
                    title: '备注',
                    key: 'Remark',
                    align: 'center',
                },
            ],
            productdata: [],
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '计划编号',
                    key: 'PlanNo',
                    align: 'center',
                },
                {
                    title: '经销商编号',
                    key: 'DealerNo',
                    align: 'center',
                },
                {
                    title: '经销商名称',
                    key: 'DealerName',
                    align: 'center',
                },
                {
                    title: '产品编号',
                    key: 'ProductNo',
                    align: 'center',
                },
                {
                    title: '产品名称',
                    key: 'ProductName',
                    align: 'center',
                },
                {
                    title: '产品规格',
                    key: 'Specification',
                    align: 'center',
                },
                {
                    title: '产品型号',
                    key: 'Type',
                    align: 'center',
                },
                {
                    title: '产品单价',
                    key: 'ProductPrice',
                    align: 'center',
                },
                {
                    title: '产品数量',
                    key: 'ProductNumber',
                    align: 'center',
                },
                {
                    title: '金额',
                    key: 'PlanMoney',
                    align: 'center',
                },
                {
                    title: '剩余数量',
                    key: 'RestNumber',
                    align: 'center',
                },
                {
                    title: '创建时间',
                    key: 'CreateTimeTemp',
                    align: 'center',
                },
                {   title: '操作',
                    key: 'action',
                    align: 'center',
                    width:200,
                    render: (h, params) => {
                        return h('div', [
                            /*h('a', {
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                }
                            },'编辑'),
                            h('span', {
                            },'|'),*/
                            h('a', {
                                style: {
                                    marginLeft: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var Store = this.Store;
                                        var Bm = this.Bm;
                                        if(Store!="运营管理中心"){
                                            toastr.warning("无该操作权限");
                                            return false;
                                        }
                                        var PlanNo = params.row.PlanNo;
                                        this.confirm(PlanNo);
                                    }
                                }
                            },'删除'),
                            h('span', {
                            },'|'),
                            h('a', {
                                style: {
                                    marginLeft: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var PlanNo = params.row.PlanNo;
                                        this.openInfoModel(PlanNo);
                                    }
                                }
                            },'查看明细')
                        ])
                    }
                }
            ],
            data1: [],
            c1Total:0,
            c1Current:1,
            c1PageSize:15,
            //查看明细弹框表格
            infocolumns: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '店面',
                    key: 'Store',
                    align: 'center',
                },
                {
                    title: '数量',
                    key: 'ProductNumber',
                    align: 'center',
                },
                {
                    title: '总金额',
                    key: 'ProductPrice',
                    align: 'center',
                }
            ],
            infodata: [],
        }
    },
    created(){
        this.initThisPage();
    },
    methods:{
        ok () {
        },
        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goLogin/initIndexPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                async:false,
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="../../login.html";
                    }else{
                        self.Store = data.Store;
                        self.Bm = data.Bm;
                        if(data.Store!="运营管理中心"){
                            $("#savePlanIsOrNot").attr("style","display:none");
                        }else{
                            $("#savePlanIsOrNot").attr("style","");
                        }
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        cancel () {
        },
        //查询经销商信息
        queryjxstable(){
            var self = this;
            var info ;
            var JxsNameOrNo = self.JxsNameOrNo;
            var jsondata={"JxsNameOrNo":JxsNameOrNo};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goJxs/queryAllJxs", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.ResultList);
                },
                error: function() {},
            });
            if(info){
                self.jxsdata = info;
            }
        },
        //查询产品信息
        queryproducttable(){
            var self = this;
            var info ;
            var ProductNameOrNo = self.ProductNameOrNo;
            var jsondata={"ProductNameOrNo":ProductNameOrNo};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProduct/queryAllProduct", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.ResultList);
                },
                error: function() {},
            });
            if(info){
                self.productdata = info;
            }
        },
        //点击经销商表格赋值
        jxsRowClic(CurrentRow){
            this.DealerNo = CurrentRow.DealerNo;
            this.DealerName = CurrentRow.Name;
            this.dealer = false;
        },
        //点击产品表格赋值
        productRowClic(CurrentRow){
            this.ProductNo = CurrentRow.ProductNo;
            this.ProductName = CurrentRow.Name;
            this.product = false;
        },
        //添加计划
        savePlan(){
            var self = this;
            var PlanNo = "JH-"+this.uuid(8, 16);
            var DealerNo = self.DealerNo;
            var DealerName = self.DealerName;
            var ProductNo = self.ProductNo;
            var ProductName = self.ProductName;
            var ProductPrice = self.ProductPrice;
            var ProductNumber = self.ProductNumber;
            var PlanMoney = self.PlanMoney;

            if(DealerNo==null||DealerNo==""||DealerName==null||DealerName==""){
                toastr.warning("请选择经销商");
                return false;
            }
            if(ProductNo==null||ProductNo==""||ProductName==null||ProductName==""){
                toastr.warning("请选择产品");
                return false;
            }
            if(ProductPrice==null||ProductPrice==""){
                toastr.warning("请录入产品价格");
                return false;
            }
            if(self.isMianJi(ProductPrice)){

            }else{
                toastr.warning("产品价格请填写两位小数以内的正数");
                return false;
            }
            if(ProductNumber==null||ProductNumber==""){
                toastr.warning("请录入产品数量");
                return false;
            }
            if(self.isPositiveInteger(ProductNumber)){}else{
                toastr.warning("产品数量请填写正整数");
                return false;
            }
            var jsondata = {
                "PlanNo":PlanNo,
                "DealerNo":DealerNo,
                "DealerName":DealerName,
                "ProductNo":ProductNo,
                "ProductName":ProductName,
                "ProductPrice":ProductPrice,
                "ProductNumber":ProductNumber,
                "PlanMoney":PlanMoney
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPlan/savePlan", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('保存成功');
                        self.PlanNo = "";
                        self.DealerNo = "";
                        self.DealerName = "";
                        self.ProductNo = "";
                        self.ProductName = "";
                        self.ProductPrice = "";
                        self.ProductNumber = "";
                        self.querytable();
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        beforequerytable(current){
            var self = this;
            self.c1Current = current;
            self.querytable();
        },
        querytable(){
            var self = this;
            var info ;
            var DealerName = self.qDealerName;
            var ProductName = self.qProductName;
            var c1Total = 0;
            var c1Current = self.c1Current;
            var c1PageSize = self.c1PageSize;
            var jsondata={"DealerName":DealerName,"ProductName":ProductName,"c1Current":c1Current,"c1PageSize":c1PageSize};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPlan/queryAllPlan", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.ResultList);
                    c1Total = data.c1Total;
                },
                error: function() {},
            });
            if(info){
                self.data1 = info;
                self.c1Total = c1Total;
            }
        },
        //是否删除计划
        confirm (PlanNo) {
            var self = this;
            this.$Modal.confirm({
                title: '提示',
                content: '<p>是否删除</p>',
                onOk: () => {
                    self.delPlan(PlanNo);
                },
                onCancel: () => {
                    toastr.info("取消删除");
                }
            });
        },
        //删除计划
        delPlan(PlanNo){
            var self = this;
            var jsondata = {
                "PlanNo":PlanNo,
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPlan/delPlan", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success("删除成功");
                        self.querytable();
                    }else{
                        toastr.error('删除失败，请刷新页面后重试');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //打开计划明细弹框
        openInfoModel(PlanNo){
            var self = this;
            self.queryStoreNumByPlanNo(PlanNo);
            self.infomodel=true;
        },
        //通过计划编号查询各店面分配数量
        queryStoreNumByPlanNo(PlanNo){
            var self = this;
            var info ;
            var jsondata={"PlanNo":PlanNo};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPlan/queryStoreNumByPlanNo", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.ResultList);
                },
                error: function() {},
            });
            if(info){
                self.infodata = info;
            }
        },

        //计算金额
        changePlanMoney(){
            this.PlanMoney = parseFloat(this.ProductPrice)*parseFloat(this.ProductNumber);
        },
        //判断两位小数
        isMianJi(mj){
            var reg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/;
            if(reg.test(mj)){
                return true;
            }else{
                return false;
            }
        },
        //判断正整数
        isPositiveInteger(s){//是否为正整数
            var re = /^[0-9]+$/ ;
            return re.test(s)
        },
        //uuid
        uuid(len, radix) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [], i;
            radix = radix || chars.length;

            if (len) {
                // Compact form
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
            } else {
                // rfc4122, version 4 form
                var r;

                // rfc4122 requires these characters
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';

                // Fill in random data. At i==19 set the high bits of clock sequence as
                // per rfc4122, sec. 4.1.5
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random()*16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }

            return uuid.join('');
        }
    }
})